define(function () {
    'use strict';
    function ctrl($scope, $state, $stateParams, $ionicLoading, CommonService) {
        function init() {
            //设置知识测评题
            //WebService.getRiskPaper().then(
            //    function (data) {
            //        for (var i = 0; i < data.length; i++) {
            //            var question = data[i];
            //            var answers = new Array();
            //            $.each(question.answer_content, function (key, value) {
            //                answers.push({"key": key, "value": value});
            //                question.answer_content = answers;
            //            });
            //            questions.paper_answer = '';
            //            question.question_idx = i + 1;
            //            $scope.questions.push(question);
            //        }
            //    }
            //);
        }

        $scope.$on('$ionicView.beforeEnter', function () {
            $scope.questions = new Array();
            $scope.result = {show: false, fraction: 0};
            init();
        });

        function readyToSubmit() {
            for (var i = 0; i < $scope.questions.length; i++) {
                var question = $scope.questions[i];
                if ('0' == question.question_kind && CommonService.isStrEmpty(question.paper_answer)) {
                    CommonService.showAlert({message: '第' + question.question_idx + '题未答！'});
                    return false;
                }
                if ('1' == question.question_kind) {
                    var answer_content = question.answer_content;
                    var paper_answer = '';
                    for (var j = 0; j < answer_content.length; j++) {
                        if (answer_content[j].checked) {
                            paper_answer += answer_content[j].key + '&';
                        }
                    }
                    if (CommonService.isStrEmpty(paper_answer)) {
                        CommonService.showAlert({message: '第' + question.question_idx + '题未答！'});
                        return false;
                    } else {
                        question.paper_answer = paper_answer.substring(0, paper_answer.length - 1);
                    }
                }
            }
            return true;
        }

        $scope.submit = function () {
            if (readyToSubmit()) {
                $ionicLoading.show();
                var paper_answers = '';
                for (var i = 0; i < $scope.questions.length; i++) {
                    var question = $scope.questions[i];
                    paper_answers += question.question_no + '&' + question.paper_answer + '|';
                }
                //isReSubmit设置为1，标示后台不做修改用户信息操作
                var params = {paper_answer: paper_answers, isReSubmit: "1", source: CommonService.getSource()};
                //获取用户测评分数设置 $scope.result.fraction,
                $ionicLoading.hide();
            }
        };
        //下一步
        $scope.next = function () {
            $state.go('tab.account-OpenRisk', {typeid: $stateParams.typeid})
        };
        //重新测评
        $scope.reset = function () {
            for (var i = 0; i < $scope.questions.length; i++) {
                var question = $scope.questions[i];
                if ('0' == question.question_kind) {
                    question.paper_answer='';
                }
                if ('1' == question.question_kind) {
                    var answer_content = question.answer_content;
                    for (var j = 0; j < answer_content.length; j++) {
                        if (answer_content[j].checked) {
                            answer_content[j].checked=false;
                        }
                    }
                }
            }
            $scope.result.show = false;
        }
    }

    ctrl.$inject = ['$scope', '$state', '$stateParams', '$ionicLoading', 'CommonService'];
    return ctrl;
});